<?php
/* SVN FILE: $Id: forums.php 191 2009-09-01 22:19:51Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Manage Forums
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 191 $
* @lastrevision $Date: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-01 22:19:51 +0000 (Tue, 01 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_admin/manage/forums.php $
*/


if ( ! defined( 'IN_ACP' ) )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'admin.php'.";
	exit();
}

class admin_ibmarket_manage_forums extends ipsCommand
{
	/**
	 * Skin object
	 *
	 * @access	private
	 * @var		object			Skin templates
	 */
	private $html;
	
	/**
	 * Main class entry point
	 *
	 * @access	public
	 * @param	object		ipsRegistry reference
	 * @return	void		[Outputs to screen]
	 */
	public function doExecute( ipsRegistry $registry )
	{
		$this->registry->getClass('class_permissions')->checkPermissionAutoMsg( 'manage_forums' );
		
		$this->html = $this->registry->output->loadTemplate('cp_skin_manage');
		
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'admin_market', 'admin_manage' ) );
		
		require_once( IPSLib::getAppDir( 'forums' ) . "/sources/classes/forums/class_forums.php" );
		$this->registry->setClass( 'class_forums', new class_forums( $this->registry ) );
		$this->registry->getClass( 'class_forums' )->forumsInit();
		
		$execute = array(
							'edit' => 'editForm',
							'save' => 'saveForums',
						);
		
		if(!isset($execute[$this->request['do']]))
		{
			$this->request['do'] = 'edit';
		}
		
		$function = $execute[$this->request['do']];
		$this->$function();
	}
	
	public function editForm()
	{
		$forumList = array();
		$this->buildForumList($forumList);
		
		$this->registry->output->html = $this->html->manageForum( $forumList );
		
		$this->registry->getClass('output')->html_main .= $this->registry->getClass('output')->global_template->global_frame_wrapper();
		$this->registry->getClass('output')->sendOutput();
	}
	
	private function buildForumList(array &$forumList, $parentID = 'root', $depth = 0)
	{
		if(is_array($this->registry->getClass('class_forums')->forum_cache[$parentID]))
		{
			foreach($this->registry->getClass('class_forums')->forum_cache[$parentID] as $forum)
			{
				$forumList[] = array($forum,$depth);
				
				$this->buildForumList($forumList, $forum['id'], $depth + 1);
			}
		}
	}
	
	public function saveForums()
	{
		ipsRegistry::getClass('adminFunctions')->checkSecurityKey();
		
		if(!is_array($this->request['forum']))
		{
			$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=forums" );
		}
		
		foreach($this->request['forum'] as $forumID => $input)
		{
			if(!is_array($this->registry->getClass('class_forums')->forum_by_id[$forumID]))
			{
				continue;
			}
			$updateForum = array(
									'market_reply' => $input['market_reply'] & 1,
									'market_reply_amount' => numbers::parseInt($input['market_reply_amount']),
									'market_topic' => $input['market_topic'] & 1,
									'market_topic_amount' => numbers::parseInt($input['market_topic_amount']),
								);
			
			$this->DB->update('forums',$updateForum,"id=".$forumID);
		}
		
		ipsRegistry::getClass('adminFunctions')->saveAdminLog( $this->lang->words['manage_forum_log'] );
		
		$this->registry->output->global_message = $this->lang->words['manage_forum_message'];
		$this->registry->output->silentRedirectWithMessage( $this->settings['base_url']."module=manage&amp;section=forums" );
	}
}